package com.dingding.queue.queue.consumer;

import java.util.Properties;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.dingding.common.core.config.Constants;
import com.dingding.common.core.config.MessageQueueConfig;
import com.dingding.common.core.config.SystemConfig.TagsEnum;
import com.dingding.queue.queue.consumer.listener.MessageListener;

/**
 * 
 * @ClassName: MessageConsumer 
 * @Description: 短信发送队列消费者
 * @author CHEHAUNBO 
 * @date 2016年6月1日 下午4:41:00
 * 
 */
@Component("messageConsumer")
public class MessageConsumer extends AbstractConsumer {

	private Logger log = Logger.getLogger(PushConsumer.class);

	@Resource(name = "messageListener")
	private MessageListener msgListener;

	@Override
	public void intital() {
		log.info("正在订阅短信主题！");
		try {
			Properties properties = new Properties();
			properties.put(PropertyKeyConst.ConsumerId, MessageQueueConfig.getMESSAGE_QUEUE_MSG_CONSUMERID());
			properties.put(PropertyKeyConst.AccessKey, Constants.MESSAGE_QUEUE_ACCESSKEY);
			properties.put(PropertyKeyConst.SecretKey, Constants.MESSAGE_QUEUE_SERECTKEY);
			Consumer consumer = ONSFactory.createConsumer(properties);
			consumer.subscribe(MessageQueueConfig.getMESSAGE_QUEUE_TOPIC(), TagsEnum.MESSAGE.toString(), msgListener);
			consumer.start();
		} catch (Exception e) {
			log.error("订阅消息队列出现异常！ MessageConsumer:intital()", e);
		}
	}

}
